import Keys from "../../config/keys";
import Config from "../../config/config";
import {AsyncStorage} from "react-native";

export default class downloadManifest{
    static isFirst = true;
    timestamp;

    GET():Promise{
        return this.fromUrl()
            .then((fromUrl) => {
                return fetch(fromUrl)
            }).then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    throw new Error('网路异常');
                }
            }).then(response => {
                AsyncStorage.setItem(Keys.downloadMainifest,JSON.stringify(response))
                    .then(()=>{
                        return AsyncStorage.setItem(Keys.downloadMainifest_time,response.timestamp);
                    });
                return response;
            });
            // .catch((e) => {
            //     console.log(e);
            // });
    }

    url():Promise {
        return AsyncStorage.getItem(Keys.downloadMainifest_time)
            .then((time) => {
                this.timestamp = time;
            })
            .then(()=>{
                return "http://config.jiankang51.cn/downloadMainifest?";
            })
    }

    fromUrl():Promise{
        return this.url()
            .then((url)=>{
                url+="productId="+Config.productId;
                url+="&pltId="+Config.pltId;
                url+="&version="+Config.version;
                if(this.timestamp && !downloadMainifest.isFirst){
                    url+="&timestamp="+this.timestamp
                }
                return url;
            })
    }



















    from(){
        return new Promise((resolve, reject) => {
            fetch(this.getUrl(), {
                method: "GET"
            })
                .then(response => {
                    if (response.ok) {
                        return response;
                    } else {
                        reject(new Error('网路异常'));
                    }
                }).then(response => {
                resolve(JSON.parse(response._bodyText));
            }).catch((e) => {
                reject(e);
            }).done();
        });
    }

    _getUrl(){
        return "http://config.jiankang51.cn/downloadMainifest?";
    }
    getUrl(){
        var and = "";
        var url = this._getUrl();
        if(!url){
            wx.showToast({title: '未找到配置接口', icon: 'none', duration: 2000});
            return;
        }
        for(let key in this){
            and+="&"+key+"="+this[key];
        }
        and = and.replace(/(^&)/g,'');
        if(and.length<=0){
            return url;
        }
        if (url.match(/(#\/)/g)){
            if (!url.match(/(\?)/g)){
                url+="?"+and;
            }else{
                url+="&"+and;
            }
        }else if (url.match(/(#)/g)){
            if (url.match(/(\?)/g)){
                url = url.replace("#","&"+and+"#");
            }else{
                url = url.replace("#","?"+and+"#");
            }
        }else {
            if (url.match(/(\?)/g)){
                url+="&"+and;
            }else{
                url=url+"?"+and;
            }
        }
        return encodeURI(url);
    }

}
